﻿using Newtonsoft.Json;
using System;

namespace ST.Common.Util.Mvc
{
	/// <summary>
	/// 转化为json时long类型转为string,否则精度会丢失
	/// </summary>
	public class LongJsonConverter : JsonConverter
	{
		public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
		{
			try
			{
				return Convert.ToInt64(reader.Value);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public override bool CanConvert(Type objectType)
		{
			return true;
		}
		public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
		{
			if (value == null)
			{
				writer.WriteNull(); return;
			}
			string sValue = value.ToString();
			writer.WriteValue(sValue);
		}
	}
}
